package com.abei.gas.service.Impl;

import com.abei.gas.entity.Employee;
import com.abei.gas.entity.Inorder;
import com.abei.gas.entity.Outorder;
import com.abei.gas.entity.Warehouse;
import com.abei.gas.mapper.EmployeeMapper;
import com.abei.gas.mapper.OutorderMapper;
import com.abei.gas.mapper.WarehouseMapper;
import com.abei.gas.service.OutorderService;
import com.abei.gas.vo.DataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OutorderServiceImpl extends ServiceImpl<OutorderMapper, Outorder> implements OutorderService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private OutorderMapper outorderMapper;

    @Override
    public DataVo stockOut(Outorder outorder) {

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("id",outorder.getEmployeeid());
        List<Employee> employeeList = employeeMapper.selectList(employeeQueryWrapper);
        if(employeeList.size()==0){
            return DataVo.error(1,"职工不存在");
        }

        QueryWrapper<Warehouse> warehouseQueryWrapper =new QueryWrapper<>();
        warehouseQueryWrapper.eq("type",outorder.getType());
        List<Warehouse> warehouseList = warehouseMapper.selectList(warehouseQueryWrapper);
        if(warehouseList.size()==0){
            return DataVo.error(1,"仓库不存在");
        }

        //库存减少
        Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
        if(warehouse.getCount() < outorder.getCount()){
            return DataVo.error(1,"库存余量不足");
        }
        warehouse.setCount(warehouse.getCount()-outorder.getCount());
        warehouseMapper.updateById(warehouse);

        outorderMapper.insert(outorder);

        return DataVo.success();
    }

    @Override
    public DataVo<Outorder> findAll(Integer page, Integer limit) {
        List<Outorder> c=outorderMapper.selectList(null);
        DataVo<Outorder> datavo=new DataVo<>();
        datavo.setCode(0);
        datavo.setMsg("");
        IPage<Outorder> cardpage=new Page<>(page,limit);
        IPage<Outorder> result=outorderMapper.selectPage(cardpage,null);
        datavo.setCount(result.getTotal());
        c=result.getRecords();
        datavo.setData(c);
        return datavo;
    }

    @Override
    public DataVo<Outorder> findById(String number) {
        QueryWrapper<Outorder> u = new QueryWrapper<>();
        u.like("id",number);
        List<Outorder> cardList=outorderMapper.selectList(u);
        DataVo<Outorder> dataVO=new DataVo<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        Integer num = outorderMapper.selectCount(null);
        dataVO.setCount(Long.valueOf(num));
        dataVO.setData(cardList);
        return dataVO;
    }
    @Override
    public void deleteById(String id) {
        Map<String,Object> columMap=new HashMap<>();
        columMap.put("id",id);
        int i=outorderMapper.deleteByMap(columMap);
        System.out.println(i);
    }
}
